<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登录情况</h1>
<div>
    <div v-if="user == ''"> <!--user等于空字符等于未登录-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-if="user != ''">
        <h1>欢迎{{user.nick}}回来!</h1>
        <a href="javascript:void(0)" @click="logout()",>登出</a>
    </div>
    <h1>{{info}}</h1>
</div>

<!--引入axios和vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            info:"",
            user:""
        },
        created:function () {
          //发出获取当前登录用户对象的请求
          axios.get("/currentUser").then(function (response) {
              v.user = response.data;
              console.log("user:"+v.user)
              v.info = response.data == "" ? "未登录" : "已登录";
          })
        },
        methods:{
            logout : function () {
                if (confirm("确定退出登录")){
                    axios.get("/logout").then(function () {
                        location.reload(); //刷新页面
                    })
                }
            }
        }
    })
</script>
</body>
</html>